
package com.assistant.codegen.modules.model.adapter.web;

import com.assistant.codegen.modules.model.app.ColumnService;
import com.assistant.codegen.modules.model.app.cmd.ColumnInsertCmd;
import com.assistant.codegen.modules.model.app.cmd.ColumnUpdateCmd;
import com.assistant.codegen.modules.model.domain.column.ModelColumn;
import com.assistant.framework.PageUtils;
import com.assistant.util.MessageBodyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/codegen")
public class ColumnController {

    @Autowired
    private ColumnService columnService;

    @GetMapping(value = "/column/{id}")
    public MessageBodyWrapper<ModelColumn> get(@PathVariable("id") Long id) {
        return MessageBodyWrapper.success(columnService.get(id));
    }

    @GetMapping(value = "/columns")
    public MessageBodyWrapper<List<ModelColumn>> list(Long modelId) {
        if (PageUtils.isPageRequest()) {
            return MessageBodyWrapper.success(PageUtils.buildToResponse(columnService.page(modelId)));
        } else {
            return MessageBodyWrapper.success(columnService.list(modelId));
        }
    }

    @PostMapping(value = "/column")
    public MessageBodyWrapper<ModelColumn> insert(@RequestBody ColumnInsertCmd cmd) {
        return MessageBodyWrapper.success(columnService.insert(cmd));
    }

    @PutMapping(value = "/column/{id}")
    public MessageBodyWrapper<Object> update(@PathVariable("id") Long id, @RequestBody ColumnUpdateCmd cmd) {
        cmd.setId(id);
        columnService.update(cmd);
        return MessageBodyWrapper.success();
    }

    @DeleteMapping(value = "/column/{id}")
    public MessageBodyWrapper<Object> delete(@PathVariable("id") Long id) {
        columnService.delete(id);
        return MessageBodyWrapper.success();
    }
}

